package com.khong.sale.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.khong.sale.R;
import com.khong.sale.entity.UserDataEntity;

public class ContactsDetailAdatpter extends BaseAdapter {
	private Context mContext;
	private List<UserDataEntity> data;
	private LayoutInflater mInflater;

	public ContactsDetailAdatpter(Context context, List<UserDataEntity> data) {
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
		this.data = data;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return data.get(position).getType();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderView holder = null;
		final UserDataEntity item = data.get(position);
		int type = getItemViewType(position);
		switch (type) {
		case 0:
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.contact_detail_phone,
						null);
				holder = new HolderView();
				holder.type = (TextView) convertView
						.findViewById(R.id.detail_typename);
				holder.key = (TextView) convertView
						.findViewById(R.id.detail_keyname);
				holder.content = (TextView) convertView
						.findViewById(R.id.detail_content);
				holder.line = (ImageView) convertView
						.findViewById(R.id.detail_line);
				holder.topLayout = (LinearLayout) convertView
						.findViewById(R.id.detail_top);
				holder.icon_1 = (ImageButton) convertView
						.findViewById(R.id.detail_btn_dial);
				holder.icon_2 = (ImageButton) convertView
						.findViewById(R.id.detail_btn_sms);
				convertView.setTag(holder);
			} else {
				holder = (HolderView) convertView.getTag();
			}
			holder.icon_1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri
							.parse("tel:" + item.getData()));
					mContext.startActivity(intent);
				}
			});
			holder.icon_2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Uri uri = Uri.parse("smsto:" + item.getData());
					Intent it = new Intent(Intent.ACTION_SENDTO, uri);
					mContext.startActivity(it);
				}
			});

			break;
		case 1:
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.contact_detail_email,
						null);
				holder = new HolderView();
				holder.type = (TextView) convertView
						.findViewById(R.id.detail_typename);
				holder.key = (TextView) convertView
						.findViewById(R.id.detail_keyname);
				holder.content = (TextView) convertView
						.findViewById(R.id.detail_content);
				holder.line = (ImageView) convertView
						.findViewById(R.id.detail_line);
				holder.topLayout = (LinearLayout) convertView
						.findViewById(R.id.detail_top);
				holder.icon_1 = (ImageButton) convertView
						.findViewById(R.id.detail_btn_email);

				convertView.setTag(holder);
			} else {
				holder = (HolderView) convertView.getTag();
			}

			holder.icon_1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Uri uri = Uri.parse("mailto:" + item.getData());
					Intent it = new Intent(Intent.ACTION_SENDTO, uri);
					// it.setType("plain/text");
					mContext.startActivity(Intent.createChooser(it,
							"Choose Email Client"));
				}
			});
			break;
		default:
			break;
		}

		switch (item.getIsFirst()) {
		case 0:
			holder.topLayout.setVisibility(View.VISIBLE);
			holder.line.setVisibility(View.GONE);
			break;
		default:
			holder.topLayout.setVisibility(View.GONE);
			holder.line.setVisibility(View.VISIBLE);
			break;
		}
		holder.type.setText(item.getTypeContent());
		holder.content.setText(item.getData());
		holder.key.setText(item.getKey());

		return convertView;
	}

	class HolderView {
		TextView type;
		TextView key;
		TextView content;
		ImageButton icon_1;
		ImageButton icon_2;
		ImageView line;
		LinearLayout topLayout;
	}
}
